package com.wn.project.mapper;

import cn.hutool.core.util.StrUtil;
import com.wn.entity.Project;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: pms
 * @ClassName ProjectMapper
 * @description:
 * @author: LRW
 * @create: 2022-10-19 15:23
 * @Version 1.0
 **/
public interface ProjectMapper {

    /***
     * @description: 新增项目
     * @param: project
     * @return: void
     * @author Lrw
     * @date: 2022/10/19 16:24
     */
    @Insert("insert into pms_project(no,name,time,principal,status) values(#{no},#{name},#{time},#{principal.id},#{status})")
    void add(Project project);

    /***
     * @description: 根据id修改项目
     * @param: project
     * @return: void
     * @author Lrw
     * @date: 2022/10/19 17:35
     */
    @UpdateProvider(type = SqlProvider.class, method = "updateProject")
    void updateById(Project project);

    /***
     * @description: 根据id查项目基本信息
     * @param: id
     * @return: com.wn.entity.Project
     * @author Lrw
     * @date: 2022/10/19 17:43
     */
    Project selectById(int id);


    /***
     * @description: 查询所有项目
     * @param:
     * @return: java.util.List<com.wn.entity.Project>
     * @author Lrw
     * @date: 2022/10/19 17:41
     */
    List<Project> selectAll();

    /***
     * @description: 多条件查询项目
     * @param: project
     * @return: java.util.List<com.wn.entity.Project>
     * @author Lrw
     * @date: 2022/10/19 17:42
     */
    List<Project> selectByCondition(Project project);

    /***
     * @description: 动态sql
     * @param: null
     * @return:
     * @author Lrw
     * @date: 2022/10/19 20:51
     */
    public static class SqlProvider {
        public static String updateProject(Project project) {
            String sql = new SQL() {
                {
                    UPDATE("pms_project");
                    if (StrUtil.isNotBlank(project.getName())) {
                        SET("name=#{name}");
                    }
                    if (StrUtil.isNotBlank(project.getNo())) {
                        SET("no=#{no}");
                    }
                    if (StrUtil.isNotBlank(project.getStatus())) {
                        SET("status=#{status}");
                    }
                    if (project.getPrincipal().getId()!=0) {
                        SET("principal=#{principal.id}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }
    }
}